#include <stdio.h>
#include <assert.h>

#include "canvas_impl.h"

// agg
#include "agg/agg_renderer_outline_aa.h"
#include "agg/agg_rasterizer_outline_aa.h"
#include "agg/interactive_polygon.h"
#include "agg/agg_conv_bspline.h"
#include "agg/agg_renderer_scanline.h"
#include "agg/agg_scanline_u.h"
#include "agg/agg_rasterizer_scanline_aa.h"

namespace visgrph
{

// typedefs
typedef agg::renderer_outline_aa<renderer_mclip>		renderer_oaa;
typedef agg::rasterizer_outline_aa<renderer_oaa>	rasterizer_outline_aa;

void canvas_impl::hline(uint x, uint y, uint length, color c)
{
	if (NULL == m_target)
		return;

	agg::rgba8 _c(c.red(), c.green(), c.blue());
	if (c.alpha() != 255)
		m_rmc.blend_hline(x, y, x + length - 1, _c, c.alpha());
	else
		m_rmc.copy_hline(x, y, x + length - 1, _c);
}

void canvas_impl::vline(uint x, uint y, uint height, color c)
{
	if (NULL == m_target)
		return;

	agg::rgba8 _c(c.red(), c.green(), c.blue());
	if (c.alpha() != 255)
		m_rmc.blend_vline(x, y, y + height - 1, _c, c.alpha());
	else
		m_rmc.copy_vline(x, y, y + height - 1, _c);
}

void canvas_impl::draw_line(float x1, float y1, float x2, float y2)
{
	// set the line width
	agg::line_profile_aa prof;
	prof.width(m_pen.width());

	renderer_oaa			ren_oaa(m_rmc, prof);
	rasterizer_outline_aa	ras_oaa(ren_oaa);

	// set cap type and join type
	ras_oaa.line_join((agg::outline_aa_join_e)m_pen.join_type());
	ras_oaa.round_cap((pen_cap_type_round == m_pen.cap_type()) ? true : false);

	// set line color
	color c(m_pen._color());
	ren_oaa.color(agg::rgba8(c.red(), c.green(), c.blue(), c.alpha()));

	// draw the line
	ras_oaa.move_to_d(x1, y1);
	ras_oaa.line_to_d(x2, y2);
	ras_oaa.render(false);
}

void canvas_impl::line(float x1, float y1, float x2, float y2)
{
	draw_line(x1, y1, x2, y2);
}

}; // namespace visgrph
/* EOF */
